package com.yl.chainStore.services.impl.warehouse

import com.yl.chainStore.entity.warehouse.WarehouseGoodSupplier
import com.yl.chainStore.mapper.warehouse.WarehouseGoodSupplierMapper
import com.yl.chainStore.services.ICompanyDictionaryService
import com.yl.chainStore.services.warehouse.IWarehouseService
import com.yl.chainStore.services.warehouse.IWarehouseSupplierService
import com.yl.common.ErrorCode
import com.yl.common.control.ResponseBody
import com.yl.common.control.returnError
import com.yl.common.control.returnSuccess
import org.slf4j.LoggerFactory
import org.springframework.stereotype.Service

@Service
class WarehouseSupplierServiceImpl(
	private val warehouseGoodSupplierMapper: WarehouseGoodSupplierMapper,

): IWarehouseSupplierService {

	lateinit var iCompanyDictionaryService: ICompanyDictionaryService
	companion object {
		private val log = LoggerFactory.getLogger(IWarehouseService::class.java)
	}

	override fun updateSupplier(supplier: WarehouseGoodSupplier): ResponseBody {
		log.info("供应商管理 ==> 信息修改 supplier:{}", supplier)
		if (supplier.name.isBlank()) {
			return returnError(ErrorCode.PARAM_EMPTY, "请输入厂商名称")
		}
		supplier.enabled = 1
		warehouseGoodSupplierMapper.updateById(supplier)
		return returnSuccess()
	}

	override fun delSupplier(id: Long): ResponseBody {
		log.info("供应商管理 ==> 删除 id:{}", id)
		val selectById =
			warehouseGoodSupplierMapper.selectById(id) ?: return returnError(ErrorCode.DATA_IS_NOT_FIND, "未找到删除对象")
		selectById.enabled = 0
		warehouseGoodSupplierMapper.updateById(selectById)
		return returnSuccess()
	}

	override fun getSupplier(id: Long): WarehouseGoodSupplier? {
		log.info("供应商管理 ==> 供应商基本信息 id:{}", id)
		val selectById = warehouseGoodSupplierMapper.selectById(id)
		if(selectById != null){
			val detail = iCompanyDictionaryService.getDetail(selectById.enterpriseNature)
			if(detail != null){
				selectById.enterpriseNatureName = detail.itemName
			}
		}

		return selectById
	}
}